//最大子数组和
class Solution {
public:
    const int MAX = 0x3f3f3f3f;
    int maxSubArray(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n+1);
        int ret = -MAX;
        for(size_t i = 1 ; i <= n ; ++i)
        {
            dp[i] = max(nums[i-1],dp[i-1]+nums[i-1]);
            if(dp[i] > ret)
                ret = dp[i];
        }
        return ret;
    }
};

//环形子数组的最大和
class Solution {
public:
    void SubarraySumCircular(vector<int>& nums,vector<int>& dp,function<int(int,int)> func,int& ret)
    {
        int n = nums.size();
        dp[0] = nums[0];
        for(size_t i = 1; i < n ; ++i)
        {
            dp[i] = func(nums[i],dp[i-1]+nums[i]);
            if(func(dp[i],ret) == dp[i])
                ret = dp[i];
        }
    }
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size();
        if(n == 1)
            return nums[0];
        vector<int> f(n);
        auto g = f;
        int retMax = INT_MIN;
        int retMin = INT_MAX;
        SubarraySumCircular(nums,f,[](int num1,int num2){if(num1 > num2) return num1;return num2;},retMax);
        SubarraySumCircular(nums,g,[](int num1,int num2){if(num1 < num2) return num1;return num2;},retMin);
        int sum = 0;
        for(auto& e : nums)
        {
            sum += e;
        }
        return sum == retMin ? retMax : max(retMax,sum-retMin);
    }
};

//乘积最大子数组
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        if(n == 1)
            return nums[0];
        vector<int> f(n);//以i位置为结尾，所有子数组中，最大乘积
        auto g = f;//以i位置为结尾，所有子数组中，最小乘积
        f[0] = nums[0];
        g[0] = nums[0];
        int retVal = f[0];
        for(size_t i = 1 ; i < n ; ++i)
        {
            f[i] = nums[i];
            int temp = max(f[i-1]*nums[i],g[i-1]*nums[i]);
            if(temp > f[i])
                f[i] = temp;
            if(f[i] > retVal)
                retVal = f[i];
            g[i] = nums[i];
            temp = min(g[i-1]*nums[i],f[i-1]*nums[i]);
            if(temp < g[i])
                g[i] = temp;
        }
        return retVal;
    }

};

//乘积为正数的最长子数组长度
class Solution {
public:
    int getMaxLen(vector<int>& nums) {
        //f(x)表示：以x位置为结尾，所有的子数组中，乘积为正数的最长长度
        //g(x)表示：以x位置为结尾，所有的子数组中，乘积为负数的最长长度
        int n = nums.size();
        if(n == 1)
        {
            if(nums[0] > 0)
                return 1;
            else 
                return 0;
        }
        vector<int> f(n);
        auto g = f;
        if(nums[0] == 0)
        {
            f[0] = 0;
            g[0] = 0;
        }
        else 
        {
            nums[0] > 0 ? (f[0] = 1 , g[0] = 0) : (f[0] = 0 , g[0] = 1);
        }
        int retVal = 0;
        for(size_t i = 1 ; i < n ; ++i)
        {
            if(nums[i] == 0)
            {
                f[0] = g[0] = 0;
                continue;
            }
            if(nums[i] > 0)
            {
                f[i] = f[i-1]+1;
                g[i] = (g[i-1] == 0 ? 0 : g[i-1]+1);
            }
            else 
            {
                f[i] = (g[i-1] == 0 ? 0 : g[i-1]+1);
                g[i] = f[i-1]+1;
            }
            if(retVal < f[i])
                retVal = f[i];
        }
        return retVal;
    }
};


